<?php

include_once ('bn_path.php');

class gridbtn
{
    public $caption = 'first,previus,next,last';
    public $enable = false;
    public $cssclass = '';
    public $page_size = 10;
}

class bn_repeater
{
    public $name = "repeater1";
    public $template = '';
    public $paging;
    public $cssclass_Table = '';
    public $cssclass_TR = '';
    public $select_color='';
    public $recivepage;
    public $div='#show';
    protected static $instance;


    public static function getInstance()
    {
        if (!isset(self::$instance))
        {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    function __construct()
    {
        $this->paging=new gridbtn;
    }

    public function Get_field_list()
    {
        preg_match_all('/<%(.*?)%>/', $this->template, $matches);
        return $matches[1];
    }

    private function paging($page, $pag)
    {
        $btn=explode(',',$this->paging->caption);
        if($this->paging->cssclass!='')
        $css=" class='".$this->paging->cssclass."'";
        $out .= "\n<td>\n";
        if ($page == 0)
        {
            $out .= "<input type='submit' value='{$btn[0]}' name='pageing'$css disabled>\n";
            $out .= "<input type='submit' value='{$btn[1]}' name='pageing'$css disabled>\n";
        } else
        {
            $out .= "<input type='submit' value='{$btn[0]}' name='pageing'$css>\n";
            $out .= "<input type='submit' value='{$btn[1]}' name='pageing'$css data-pnumber='" . ($page -1) . "'>\n";
        }
        if ($pag == $page || ($page == 0 && $pag == 0))
        {
            $out .= "<input type='submit' value='{$btn[2]}' name='pageing'$css disabled>\n";
            $out .= "<input type='submit' value='{$btn[3]}' name='pageing'$css disabled>\n";
        } else
        {
            $out .= "<input type='submit' value='{$btn[2]}' name='pageing'$css data-pnumber='" . ($page +1) . "'>\n";
            $out .= "<input type='submit' value='{$btn[3]}' name='pageing'$css data-pnumber='" . ($pag) ."'>\n";
        }
        $out .= "<input type='hidden' name='" . $this->name . "_page' id='" . $this->name . "_page' value='$page'/>\n</td>\n";
        return $out;
    }

    public function show($datasource,$page=0)
    {
        $out = '';
        if (isset($datasource))
        {
            if ($this->cssclass_Table != '')
                $this->cssclass_Table = ' class="' . $this->cssclass_Table . '"';
            $out = "<table border='1'$this->cssclass_Table id='$this->name' name='$this->name'>";
            $maches = self::get_field_list();
            $out .="\n<tfoot>\n<tr>";
            if ($this->paging->enable)
            {
                $j = count($datasource);
                $pag = intval($j / $this->paging->page_size);
                $rem = $j % $this->paging->page_size;
                if ($rem == 0)
                    --$pag;
                $out .= self::paging($page, $pag);    
                $datasource = array_slice($datasource, $this->paging->page_size * $page, $this->paging->page_size);
            }
            $out .="\n</tr>\n</tfoot>\n<tbody>";
            foreach ($datasource as $row)
            {
                if (is_object($row))
                    $row = (array )$row;
                $out .= "\n<tr>\n<td>";
                $cell = $this->template;
                foreach ($maches as $field)
                {
                    $pos = strpos($field, '(');
                    if ($pos != false)
                    {
                        $func = trim(substr($field, 0, $pos));
                        $arg = trim(substr($field, $pos + 1, -1));
                        $cell = str_replace("<%$field%>", call_user_func($func, $row[$arg]), $cell);
                    } else
                        $cell = str_replace("<%$field%>", $row[trim($field)], $cell);
                }
                $out .= $cell . "</td>\n</tr>\n";
            }
                $out .= "\n</tbody>\n</table>";
        }
        echo $out;
    }

    function command_script($divid)
    {
        if ($this->recivepage == '')
            $this->recivepage = 'window.location.href';
        else
        $this->recivepage="'$this->recivepage'";
        $out .= "$('input[name=pageing]').live('click',function(){ 
                    var pid = $(this).data('pnumber');
                    $('#" . $this->name . "_page').val(pid);
                    $.post($this->recivepage,'cmd=paging&'+$('$divid :input').serialize(),function(data){
                    $('$this->div').html(data);
                    });
                    });";
                if($this->select_color!='')
        $out.="
        $('#$this->name tr').hover(function()
        {
            if($(this).parent().is('tbody'))
        $(this).css('background-color','$this->select_color'); 
        },function(){
        $(this).css('background-color','');    
        });";
        return $out;
    }

}

?>